package com.zgkxzx.sourceanasys;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.zgkxzx.sourceanasys.retrofittest.ApiManager;
import com.zgkxzx.sourceanasys.retrofittest.ApiService;

import java.io.IOException;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;


public class RetrofitTestActivity extends AppCompatActivity {

    private Button btnOkHttpTest;
    private Button btnRetrofitTest;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit_test);
        btnOkHttpTest = (Button) findViewById(R.id.btn_okhttp_test);
        btnRetrofitTest = (Button) findViewById(R.id.btn_retrofit_test);


        btnOkHttpTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                okHttpTest("13058023282","123456");
            }
        });
        btnRetrofitTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                retrofitTest("13058023282","123456");
            }
        });

    }


    private void okHttpTest(String phoneNo, String pwd) {
        RequestBody requestBody = new FormBody.Builder()
                .add("phoneNo", phoneNo)
                .add("pwd", pwd)
                .build();

        //构造Request
        Request req = new Request.Builder()
                .url("www.xxx.com")
                .post(requestBody)
                .build();
        //构造一个HttpClient
        OkHttpClient client = new OkHttpClient();
        //发送请求
        client.newCall(req)
                .enqueue(new Callback() {
                    //获得服务器返回的Response数据
                    @Override
                    public void onResponse(Call arg0, Response response) throws IOException {
                        //获取响应体
                        ResponseBody body = response.body();
                        //响应体获取到string
                        String requestBody = body.string();
                        //通过gson解析器解析成对应的对象实体
                        Object o = new Gson().fromJson(requestBody, Object.class);
                        //处理 还会有线程切换

                    }

                    @Override
                    public void onFailure(Call arg0, IOException arg1) {
                        // TODO Auto-generated method stub

                    }
                });
    }

    private void retrofitTest(String phoneNo, String pwd) {
        ApiManager.getInstance().initRetrofit().create(ApiService.class).login(phoneNo, pwd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });


    }
}
